﻿using System;
using System.Collections.Generic;
using System.Linq;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using UnityEngine.Serialization;
using Yoozoo.UI.YGUI;
using Yoozoo.Mars.Got;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{
    [Serializable]
    public class YListViewAdaptConfig
    {
        public Vector2 itemSpace; //item间距
        public int _layout;
        public int _verticalAlign;
        public int _align;
        public bool enbale = true;
        public List<string> _itemsSizeDictNames = new List<string>();
        [FormerlySerializedAs("_ItemsSizeDictVector2s")] public List<Vector2> _itemsSizeDictVector2s = new List<Vector2>();

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            YListViewAdaptConfig other = (YListViewAdaptConfig)obj;

            if (enbale != other.enbale)
            {
                return false;
            }

            // Compare itemSpace properties
            if (itemSpace != other.itemSpace)
            {
                return false;
            }

            // Compare _layout properties
            if (_layout != other._layout)
            {
                return false;
            }

            // Compare _verticalAlign properties
            if (_verticalAlign != other._verticalAlign)
            {
                return false;
            }

            // Compare _align properties
            if (_align != other._align)
            {
                return false;
            }

            if (!_itemsSizeDictNames.SequenceEqual(other._itemsSizeDictNames))
            {
                return false;
            }

            if (!_itemsSizeDictVector2s.SequenceEqual(other._itemsSizeDictVector2s))
            {
                return false;
            }

            return true;
        }

        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + itemSpace.GetHashCode();
            hash = hash * 23 + _layout.GetHashCode();
            hash = hash * 23 + _verticalAlign.GetHashCode();
            hash = hash * 23 + _align.GetHashCode();
            hash = hash * 23 + enbale.GetHashCode();
            hash = hash * 23 + _itemsSizeDictNames.GetHashCode();
            hash = hash * 23 + _itemsSizeDictVector2s.GetHashCode();
            return hash;
        }
    }
    public class YListViewAdapt:AdaptBase<YListViewAdaptConfig, ListView>
    {
        public override int Order => (int)AdapterExecuteOrder.ListView; 
        protected override void ApplyConfig(YListViewAdaptConfig config)
        {
            base.ApplyConfig(config);
            // 横竖切换旧的firstIndex 必须此处存下
            int curIndex = mComponent._firstIndex;
            mComponent.enabled = config.enbale;
            mComponent.ItemSpace = config.itemSpace; 
     

            var _itemsSizeDict = new Dictionary<string, Vector2>(config._itemsSizeDictNames.Count);
            for (int i = 0; i < config._itemsSizeDictNames.Count; i++)
            {
                if (_itemsSizeDict.ContainsKey(config._itemsSizeDictNames[i]))
                {
                    _itemsSizeDict[config._itemsSizeDictNames[i]] = config._itemsSizeDictVector2s[i];
                }
                else
                {
                    _itemsSizeDict.Add(config._itemsSizeDictNames[i], config._itemsSizeDictVector2s[i]);
                }
            }
          
            mComponent.ScreenOrientationChangeResetSizePool(_itemsSizeDict);
            //更新ItemSize之后再刷新Layout,防止出现类似 竖版的List使用横版的ItemSize来计算列数 的境况
            mComponent.SetLayoutAndAlign(config._layout, config._verticalAlign, config._align);
            // mComponent.ScrollToView(curIndex-1);
            // TODO 贪狼 此处横竖分辨率切换时候，宽高那一帧不对，只要是ListView带锚点 适配UIROOT 就会横竖自由切换时首帧宽高不对， 导致计算错误
            // 暂时横竖切换时 先ScrollTo 0
            mComponent.ResetContentPos();
        }

        protected override void CopyProperty(YListViewAdaptConfig config)
        {
            base.CopyProperty(config);
            config.enbale = mComponent.enabled;
            config.itemSpace = mComponent.ItemSpace;
            config._layout = mComponent.layout;
            config._verticalAlign = mComponent.verticalAlign;
            config._align = mComponent.align;
            
            if (mComponent.m_ViewCells.Length == 0)
            {
                Debug.LogError("[Listview] Item cells not exist.");
            }
            else
            {
                 List<Vector2> __ItemsSizeDictVector2s_Portrait = new List<Vector2>();
                 List<string> __itemsSizeDictNames = new List<string>();
                 List<Vector2> __ItemsSizeDictVector2s = new List<Vector2>();
                for (var i = 0; i < mComponent.m_ViewCells.Length; i++)
                {
                    if (mComponent.m_ViewCells[i] == null)
                    {
                        Debug.LogError("[Listview] Item cells is null.");
                        return;
                    }
                    ListViewItemAdapt _viewCellsAdapt = mComponent.m_ViewCells[i].GetComponent<ListViewItemAdapt>();
                    var size = mComponent.m_ViewCells[i].GetComponent<RectTransform>().rect.size;
                    var size_Portrait = size;
                    if (_viewCellsAdapt != null)
                    {
                        size = _viewCellsAdapt.GetListViewItemSizeByScreenOrient(true);
                        size_Portrait = _viewCellsAdapt.GetListViewItemSizeByScreenOrient(false);
                    }
                    
                    string identifyName = mComponent.m_ViewCells[i].identifyName;
                    if (string.IsNullOrEmpty(identifyName))
                    {
                        Debug.LogError("[Listview] Item cells identifyName is null.");
                    }
                    __itemsSizeDictNames.Add(identifyName);
                    __ItemsSizeDictVector2s.Add(size);
                    __ItemsSizeDictVector2s_Portrait.Add(size_Portrait);
                }
                
                // ListViewItem 如果存在横竖屏size不同，ListView此处保存时，将Item横竖屏的配置都保存下来，不用再重复切换分辨率保存
                if (LandscapeConfig != null)
                {
                    LandscapeConfig._itemsSizeDictNames = __itemsSizeDictNames;
                    LandscapeConfig._itemsSizeDictVector2s = __ItemsSizeDictVector2s;
                }
                if (PortraitConfig != null)
                {
                    PortraitConfig._itemsSizeDictNames = __itemsSizeDictNames;
                    PortraitConfig._itemsSizeDictVector2s = __ItemsSizeDictVector2s_Portrait;
                }
            }
        }
    }
}